<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">入库单号</label>
        <el-input v-model.trim="query.inboundOrderNo" clearable placeholder="入库单号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" @clear="crud.toQuery" />
        <!-- <label class="el-form-item-label">入库任务类型</label>
        <el-select v-model="query.inboundTaskType" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option />
        </el-select> -->
        <label class="el-form-item-label">入库任务状态</label>
        <el-select v-model="query.status" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;" @change="crud.toQuery">
          <el-option
            v-for="item in dict.allOrder_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">创建时间</label>
        <el-date-picker
          v-model="query.createTime"
          class="filter-item"
          style="width: 185px;"
          type="date"
          placeholder="请选择"
          value-format="yyyy-MM-dd"
          @change="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <crudOperation :permission="permission">
        <template slot="right">
          <el-upload
            ref="uploadRef"
            slot="right"
            class="filter-item"
            :headers="headers"
            :action="uploadUrl"
            :limit="1"
            :show-file-list="false"
            :http-request="customUpload"
          >
            <el-button
              type="success"
              plain
              icon="el-icon-upload2"
              size="mini"
              :loading="btn.upload"
              @click="handleUpload"
            >导入</el-button>
          </el-upload>
        </template>
      </crudOperation>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="inboundOrderNo" label="入库单号" />
        <el-table-column prop="inboundTaskNo" label="任务编号" />
        <el-table-column prop="parentPalltetCode" label="母托编码" />
        <el-table-column prop="bandingType" label="打带类型" />
        <el-table-column prop="wrappingQty" label="裹膜圈数" />
        <el-table-column prop="status" label="入库任务状态">
          <template slot-scope="scope">
            {{ dict.label.allOrder_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="taskStart" label="任务起点" />
        <el-table-column prop="taskEnd" label="任务终点" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="finishTime" label="完成时间" />
        <!-- <el-table-column v-if="checkPer(['admin','inboundTask:edit','inboundTask:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column> -->
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import crudInboundTask from '@/api/work/inBoundWork'
import { uploadFileAPI } from '@/api/work/inBoundWork'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, inboundOrderNo: null, inboundTaskType: null, status: null, priority: null, createTime: null, finishTime: null }
export default {
  name: 'InBoundWork',
  components: { pagination, rrOperation, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['allOrder_status'],
  cruds() {
    return CRUD({ title: 'inboundTask', url: 'api/inboundTask', idField: 'id', sort: 'id,desc', crudMethod: { ...crudInboundTask },
      optShow: {
        add: false,
        edit: false,
        del: false,
        reset: true,
        download: true
      }
    })
  },
  data() {
    return {
      list: [
        {
          inboundOrderNo: 'inboundOrderNo001',
          inboundTaskType: 'type1',
          status: 1,
          priority: 999,
          createTime: '2021-01-01',
          finishTime: '2021-01-02'
        }
      ],
      permission: {
        add: ['admin', 'inboundTask:add', 'otherUser'],
        edit: ['admin', 'inboundTask:edit', 'otherUser'],
        del: ['admin', 'inboundTask:del', 'otherUser']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'inboundOrderNo', display_name: '入库单号' },
        { key: 'inboundTaskType', display_name: '入库任务类型' },
        { key: 'status', display_name: '入库任务状态' },
        { key: 'createTime', display_name: '创建时间' }
      ],
      uploadUrl: process.env.VUE_APP_BASE_API + '',
      headers: {
        'Authorization': getToken(),
        'Content-Type': 'multipart/form-data'
      },
      btn: {
        upload: false
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async customUpload(file) {
      const formData = new FormData()
      formData.append('file', file.file)
      try {
        this.btn.upload = true
        const res = await uploadFileAPI({
          formData: formData,
          ruleType: this.selectedValue
        })
        if (res.result === 'OK') {
          this.$message.success(res.data)
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.upload = false
      }
    },
    // 清除已选择的文件
    handleUpload() {
      this.$refs.uploadRef.clearFiles() // 清除已选择的文件
      this.$refs.uploadRef.submit() // 重新提交上传
    }
  }
}
</script>

<style scoped>

</style>
